package ruru.client.ui.widgets;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.ClickListenerCollection;
import com.google.gwt.user.client.ui.KeyboardListener;
import com.google.gwt.user.client.ui.KeyboardListenerCollection;
import com.google.gwt.user.client.ui.MouseListener;
import com.google.gwt.user.client.ui.MouseListenerCollection;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.SourcesClickEvents;
import com.google.gwt.user.client.ui.SourcesMouseEvents;
import com.google.gwt.user.client.ui.Widget;

/**
 * This class differs from FocusPanel in the following ways:
 * 1)	does not implement hasFocus or any of the iterface's methods
 * 2) removed references to focus events
 * 3) removed focusListenerCollection
 * 4) removed onbrowserevent: ONFOCUS
 * 5) super(); instead of super(impl.createFocusable()); in constructor
 * 
 * @author zane.tian@gmail.com
 */
public class NoFocusMouseListenerPanel extends SimplePanel implements SourcesClickEvents, SourcesMouseEvents {
	private ClickListenerCollection clickListeners;
	private KeyboardListenerCollection keyboardListeners;
	private MouseListenerCollection mouseListeners;

	public NoFocusMouseListenerPanel() {
		super();
		sinkEvents(Event.KEYEVENTS | Event.ONCLICK | Event.MOUSEEVENTS);
	}

	public NoFocusMouseListenerPanel(Widget child) {
		this();
		add(child);
	}

	public void addClickListener(ClickListener listener) {
		if(clickListeners == null)
			clickListeners = new ClickListenerCollection();
		clickListeners.add(listener);
	}

	public void addKeyboardListener(KeyboardListener listener) {
		if(keyboardListeners == null)
			keyboardListeners = new KeyboardListenerCollection();
		keyboardListeners.add(listener);
	}

	public void addMouseListener(MouseListener listener) {
		if(mouseListeners == null)
			mouseListeners = new MouseListenerCollection();
		mouseListeners.add(listener);
	}

	public void onBrowserEvent(Event event) {
		switch(DOM.eventGetType(event)) {
		case Event.ONCLICK:
			if(clickListeners != null)
				clickListeners.fireClick(this);
			break;

		case Event.ONMOUSEDOWN:
		case Event.ONMOUSEUP:
		case Event.ONMOUSEMOVE:
		case Event.ONMOUSEOVER:
		case Event.ONMOUSEOUT:
			if(mouseListeners != null)
				mouseListeners.fireMouseEvent(this, event);
			break;

		case Event.ONBLUR:

		case Event.ONKEYDOWN:
		case Event.ONKEYUP:
		case Event.ONKEYPRESS:
			if(keyboardListeners != null)
				keyboardListeners.fireKeyboardEvent(this, event);
			break;
		}
	}

	public void removeClickListener(ClickListener listener) {
		if(clickListeners != null)
			clickListeners.remove(listener);
	}

	public void removeKeyboardListener(KeyboardListener listener) {
		if(keyboardListeners != null)
			keyboardListeners.remove(listener);
	}

	public void removeMouseListener(MouseListener listener) {
		if(mouseListeners != null)
			mouseListeners.remove(listener);
	}
}
